<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          default-expand-all=""
          :data="list"
        >
          <!-- 使用作用域插槽，获取组件的数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <!-- 编辑部门需要获取id，使用插槽获取id -->
                        <el-dropdown-item @click.native="hedit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hdel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 添加弹框，用弹框包裹子组件 -->
      <el-dialog
        :title="isEdit? '编辑' : '添加'"
        :visible.sync="showDialog"
      >
        <!-- 子组件 -->
        <deptDialog
          v-if="showDialog"
          :id="curId"
          :origen-list="origenList"
          :is-edit="isEdit"
          @close="showDialog=false"
          @success="hsuccess"
        />
      </el-dialog></div>
  </div>
</template>
<script>
import deptDialog from './deptDialog.vue'
import { treeData } from '@/utils/index'
import { getDepartment, delDepartment } from '@/api/departments'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      // list 的数据给到了<el tree> 组件里面，要想获取组件的数据需要用到插槽，这里是作用域插槽
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      origenList: []
    }
  },
  created() {
    // 一进到页面就要看到数据，所以初始化之后就调用请求数据的函数
    this.loadDepartment()
  },
  methods: {
    // 删除部门
    async hdel(id) {
      await delDepartment(id)
      // console.log(res)
      this.loadDepartment()
    },
    // 触发子组件的添加事件，渲染页面
    hsuccess() {
      this.loadDepartment()
      this.showDialog = false
    },
    // 编辑
    async hedit(id) {
      this.showDialog = true
      this.isEdit = true
      this.curId = id
    },
    // 添加
    hadd(id) {
      this.showDialog = true
      this.isEdit = false
      this.curId = id
    },
    async loadDepartment() {
      // 发送请求
      const res = await getDepartment()
      try {
        // console.log(res)
        res.data.depts.shift()
        this.list = treeData(res.data.depts)
        // console.log(res.data.depts)
        // this.origenList 通过父传子，在子组件校验code的唯一性
        this.origenList = res.data.depts.map(item => {
          const { id, pid, name, code } = item
          return { id, pid, name, code }
        })
        // console.log(this.origenList)
      } catch (error) {
        console.log(error)
      }
    }
  }
}

</script>
